package com.sphd.emanu.activity.goodslist;

import com.google.gson.JsonObject;

import com.sphd.emanu.mvp.BasePresenterImpl;
import com.sphd.emanu.pojo.goods.Commodity;
import com.sphd.emanu.utils.GsonUtil;
import com.wzutils.JsonUtil;
import com.wzutils.L;

import java.util.List;

import cn.pedant.SweetAlert.SweetAlertDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * MVPPlugin
 *  邮箱 784787081@qq.com
 */

public class GoodslistPresenter extends BasePresenterImpl<GoodslistContract.View> implements GoodslistContract.Presenter{

    @Override
    public void getList(int id) {
        Call<String> call =emanuService.selectCommodity(id);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                List<Commodity> commodityList =GsonUtil.jsonToList(response.body().toString(),Commodity.class);
              mView.setList(commodityList);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Error(t);
            }
        });
    }

    @Override
    public void delGood(Integer goodsEntityId, final SweetAlertDialog sweetAlertDialog, final int pos) {
        Call<JsonObject> call =emanuService.deleteCommodity(goodsEntityId);
        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                L.e(response.toString());
                int delRes=response.body().get("resStatus").getAsInt();
                mView.setDelRes(delRes,sweetAlertDialog,pos);
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                Error(t);
            }
        });
    }
}
